//
//  UpLevelGV.swift
//  demo01
//
//  Created by babybus on 2025/7/7.
//
import Foundation
import UIKit
import SnapKit
import Alamofire

class UpLevelGVView:UIView{
    //全局变量
//    private var topOfBody:TopOfBodyGVView = TopOfBodyGVView()
    private let api = "/growthvalue/level"
    private var levelInfo:LevelInfo!
    private var unlockTimesView:UIView = UIView()
    private let levelLabel:UILabel={
        let label = UILabel()
        label.textColor = .white
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 25, weight: .bold)
        label.adjustsFontSizeToFitWidth = true // 开启自动缩放
        label.minimumScaleFactor = 0.5 // 最小缩放比例（默认0.5）
        return label
    }()
    private var actualLevel:Int = 0
    private var displayLevel:Int = 0
    private let expLabel:UILabel={
        let label = UILabel()
        label.textColor = .systemGray5
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 12, weight: .bold)
        label.adjustsFontSizeToFitWidth = true // 开启自动缩放
        label.minimumScaleFactor = 0.5 // 最小缩放比例（默认0.5）
        return label
    }()
    private let progressView:UIProgressView={
        let progress = UIProgressView(progressViewStyle: .default)
        progress.progressTintColor = .orange     // 进度条颜色
        progress.trackTintColor = .systemGray6  // 背景轨道颜色
        progress.layer.cornerRadius = 6
        progress.layer.masksToBounds = true
        return progress
    }()
    //初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        loadData(userId: 1)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        actualLevel = levelInfo.level
        displayLevel = levelInfo.level
        levelLabel.text = "LV" + String(displayLevel)
        
        expLabel.text = "升级进度" + String(levelInfo.currentExp) + "/" + String(levelInfo.sumExp)
        progressView.progress = Float(levelInfo.currentExp) / Float(levelInfo.sumExp)  // 设置进度值(0.0~1.0)
        setUnlockTimesView()
        
        self.addSubview(levelLabel)
        self.addSubview(expLabel)
        self.addSubview(progressView)
        self.addSubview(unlockTimesView)
        
        levelLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(70)
            make.height.equalTo(36)
            make.top.equalToSuperview()
        }
        expLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
//            make.width.equalTo(111)
            make.height.equalTo(16)
            make.top.equalTo(levelLabel.snp.bottom).offset(4)
        }
        progressView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(306)
            make.height.equalTo(12)
            make.top.equalTo(expLabel.snp.bottom).offset(6)
        }
        unlockTimesView.snp.makeConstraints { make in
            make.width.equalTo(128)
            make.height.equalTo(20)
            make.top.equalToSuperview()
            make.right.equalToSuperview().inset(22)
        }
        
    }
    func loadData(userId:Int){
        let url = AllUrl.LH_URL + api + "?userId=\(userId)"
        AF.request(url,method: .get)
            .validate(statusCode: 200..<300)
            .responseDecodable(of:LevelInfo.self){ response in
                switch response.result{
                case .success(let level):
                    DispatchQueue.main.async {
                        self.levelInfo = LevelInfo(level: level.level, currentExp: level.currentExp, sumExp: level.sumExp)
                        self.setupUI()
                    }
                case .failure(let error):
                    print("错误 -- topBarGV: \(error.localizedDescription)")
                }
            }
    }
    func setUnlockTimesView(){
        unlockTimesView.backgroundColor = .orange
        unlockTimesView.layer.cornerRadius = 10
        unlockTimesView.layer.masksToBounds = true
        unlockTimesView.backgroundColor = ColorTool.getGradientFromTo(from: UIColor(red: 253/255, green: 182/255, blue: 96/255, alpha: 1.0),
                                                                      to: UIColor(red: 252/255, green: 64/255, blue: 106/255, alpha: 1.0))
        let titleLabel = UILabel()
        let smallButton = UIButton()
        
        titleLabel.text = "升级得10个解锁次数"
        titleLabel.textColor = .white
        titleLabel.font = UIFont.systemFont(ofSize: 12)
        titleLabel.adjustsFontSizeToFitWidth = true // 开启自动缩放
        titleLabel.minimumScaleFactor = 0.5 // 最小缩放比例（默认0.5）
        
        smallButton.setImage(UIImage(named: "smallArrow"), for: .normal)
        
        unlockTimesView.addSubview(titleLabel)
        unlockTimesView.addSubview(smallButton)
        
        titleLabel.snp.makeConstraints { make in
            make.width.equalTo(109)
            make.height.equalTo(14)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().inset(6)
        }
        smallButton.snp.makeConstraints { make in
            make.width.equalTo(5)
            make.height.equalTo(8)
            make.centerY.equalToSuperview()
            make.right.equalTo(titleLabel.snp.right).offset(6)
        }
    }
    
}
extension UpLevelGVView: TopOfBodyGVView.LevelDelegate {
    func nextLevel(){
        displayLevel+=1
        if displayLevel > 50{
            displayLevel = 50
            return
        }
        judgeLevel()
        levelLabel.text = "LV" + String(displayLevel)
    }
    func previousLevel(){
        displayLevel-=1
        if displayLevel < 0{
            displayLevel = 0
            return
        }
        judgeLevel()
        levelLabel.text = "LV" + String(displayLevel)
    }
    func judgeLevel(){
        if displayLevel == actualLevel{
            expLabel.text = "升级进度" + String(levelInfo.currentExp) + "/" + String(levelInfo.sumExp)
        }else{
            expLabel.text = "目前等级lv" + String(actualLevel) + ",升级进度" + String(levelInfo.currentExp) + "/" + String(levelInfo.sumExp)
        }
    }
}
